package cn.edu.zjut.mina.service.impl;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.edu.zjut.mina.bean.RoomInfo;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.interf.IRoomInfo;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.service.IEveryGameBeginService;
import cn.edu.zjut.mina.service.INotificationService;
import cn.edu.zjut.mina.service.IUserPrepareService;
import cn.edu.zjut.mina.util.DateUtils;
import cn.edu.zjut.mina.util.GsonUtils;
import cn.edu.zjut.mina.util.UserSessionMap;
import cn.edu.zjut.mina.util.ResultInfoConstUtils;

@Service("userPrepareService")
public class UserPrepareServiceImpl implements IUserPrepareService {

	/**
	 * 用于存放用户信息的集合
	 */
	private final UserSessionMap userSessionMap = UserSessionMap.getInstance();

	@Qualifier("notificationService")
	@Autowired
	private INotificationService notificationService;

	@Qualifier("everyGameBeginService")
	@Autowired
	private IEveryGameBeginService everyGameBeginService;

	public JMessageProtocalRes prepare(StandardFormat inJson, IoSession session) {
		// TODO Auto-generated method stub
		System.out.println(DateUtils.getTime() + "execute --Prepare()-- method.");

		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		IRoomInfo roomInfo = userSessionMap.get(inJson.roomId);

		if (roomInfo == null) {// 房间号不存在
			System.out.println(DateUtils.getTime() + "房间号不存在" + inJson.roomId);
			outJson.userId = inJson.userId;
			outJson.resultInfo = ResultInfoConstUtils.PREPARE_ROOMNULLINFO + inJson.roomId;
			outJson.result = false;
		} else {
			synchronized (roomInfo) {// 参考java编程思想 689页
				// 发出请求的玩家准备
				for (int index = 0; index < roomInfo.getActualParticipation(); index++) {
					if (roomInfo.getUserSessions().get(index).user.getUserId().equals(inJson.userId)) {
						roomInfo.getUserSessions().get(index).user.setPrepare(1);
						break;
					}
				}

				// 计算当前准备人数
				if (roomInfo.getParticipation() == roomInfo.getActualParticipation()) {
					roomInfo.setActualPrepare(0);
					for (int index = 0; index < roomInfo.getParticipation(); index++) {
						if (roomInfo.getUserSessions().get(index).user.getPrepare() == 1)
							roomInfo.setActualPrepare(roomInfo.getActualPrepare() + 1);
					}
				} else
					roomInfo.setActualPrepare(0);

				if (roomInfo.getParticipation() > roomInfo.getActualPrepare()) {// 通知所有人准备
					// roomInfo.setActualPrepare(roomInfo.getActualPrepare() +
					// 1);// 已准备人数加一
					notificationService.notificationPrepare(inJson.roomId, inJson.userId);// 通知该房间的所有人
					return null;

				} else {
					if (!roomInfo.isActualGameStart()) {
						notificationService.notificationPrepare(inJson.roomId, inJson.userId);// 通知该房间的所有人
						// 如果是最后一个人准备，则直接发牌
						switch (roomInfo.getGameType()) {
						case 0:
							everyGameBeginService = new EveryGameBeginServiceImpl();
							break;
						case 1:
							everyGameBeginService = new EveryGameBeginServiceImpl_xcmj();
							break;
						default:
							everyGameBeginService = new EveryGameBeginServiceImpl();
						}
						everyGameBeginService.start(inJson, null);
					}
					return null;
				}
			}
		}
		res.setResultCode(ProtocalRegulation.PREPAREINITIATIVE);
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}

}
